<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <code-tech.diandian.com>
// +----------------------------------------------------------------------

namespace App\Controller;
use Think\Controller;
use OT;
use User\Api\UserApi;
//司机
class DriverController extends AppController{

    //司机端注册
    public function driverSign(){

        $config=array(
            'allowExts'=>array('jpg','png','gif'),
            'savePath'=>'Picture/',
            'saveName' => array('uniqid',''),
            'saveRule'=>'time',
            //'uploadReplace ' => false,
        );



      /*  $driver = D('Driver');
        $test['idcard'] = $info[0]['savepath'].'/'.$info[0]['savename'];
        $driver->add($test);*/

       // file_put_contents('Uploads/Picture/idcard.png',base64_decode(I('image1')));


        $map['driver_tel']    = I('phone');
        $map['realname']      = I('name');
        $map['vehicle_type']  = I('models');
        $map['vehicle_brand'] = I('brand');
        $map['vehicle_model'] = I('version');
        $map['vehicle_width']       = I('licenseck');
        $map['license_plate'] = I('licenseplate');
        $map['tonnage']       = I('licensezz');
        $map['vehicle_height']     = I('licensecg');
        $map['vehicle_length']     = I('vehicle_length');
        $map['status']        = 0;
        $member['username']        = I('name');
        $member['mobile']          = I('phone');
        $member['password']        = md5(sha1(I('password')) . UC_AUTH_KEY);
        $member['status']     = 1;
        $member['last_login_ip'] = get_client_ip();
        $ucenterMember = M('UcenterMember');
        $map['id']     = $ucenterMember->add($member);
       /* $this->ajaxReturn($map,'JSON');
        exit;*/
       /* $User       =   new UserApi;

        $map['id']  =   $User->register($map['realname'],$member['password'],'12332@qq.com',$map['driver_tel']);*/

        if($map['id']>0){
            $upload = new \Think\Upload($config);//3.1版本之前是UploadFile，所以这里修改下
            $info = $upload->upload();
            if (!$info) {
                $data['success'] = false;
                $data['msg']     = $upload->getError();
                $this->ajaxReturn($data,'JSON');
            }
            $map['driver_photos']          = 'Uploads/'.$info[1]['savepath'].$info[1]['savename'];
            $map['idcard']                 = 'Uploads/'.$info[2]['savepath'].$info[2]['savename'];
            $map['drivers_license']        = 'Uploads/'.$info[3]['savepath'].$info[3]['savename'];
            $map['vehicle_photos']         = 'Uploads/'.$info[4]['savepath'].$info[4]['savename'];
            $map['driving_license']        = 'Uploads/'.$info[5]['savepath'].$info[5]['savename'];
            $map['operation_certificate']  = 'Uploads/'.$info[6]['savepath'].$info[6]['savename'];
            $driver = D('Driver');
            $rst = $driver->add($map);
            if($rst){
                $data['success'] = true;
                $data['msg']     = '注册成功';
                $data['obj'] = $map;
            }else{
                $data['success'] = false;
                $data['msg']     = $this->showRegError($map['id']);

            }

        }else{
            $data['success'] = false;
            $data['msg']     = $this->showRegError($map['id']);

        } 
       /* $data['success'] = true;
        $data['msg']     = '注册成功';*/
       $this->ajaxReturn($data,'JSON');
       //dump($map);

    }
    //司机登录
    public function login($username = null, $password = null, $verify = null){
        if(IS_POST){

            /* 调用UC登录接口登录 */
            $User = new UserApi;
            $uid = $User->login($username, $password);
            $driver = D("Driver");
            $user = $driver->find($uid);
            if(0 < $uid){ //UC登录成功(账号密码验证成功)
                $data['success'] = true;
                $data['msg']     = '登陆成功';
                $data['obj'] = $user;
                $this->ajaxReturn($data);
            } else { //登录失败

                switch($uid) {
                    case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
                    case -2: $error = '密码错误！'; break;
                    default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
                }
                $data['success'] = false;
                $data['msg']     = $error;
                $this->ajaxReturn($data);
            }

        } else {
            $data['success'] = false;
            $data['msg']     = '请使用GET方法登陆';
            $this->ajaxReturn($data);
        }
    }
    /**
     * 获取用户注册错误信息
     * @param  integer $code 错误编码
     * @return string        错误信息
     */
    private function showRegError($code = 0){
        switch ($code) {
            case -1:  $error = '用户名长度必须在16个字符以内！'; break;
            case -2:  $error = '用户名被禁止注册！'; break;
            case -3:  $error = '用户名被占用！'; break;
            case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
            case -5:  $error = '邮箱格式不正确！'; break;
            case -6:  $error = '邮箱长度必须在1-32个字符之间！'; break;
            case -7:  $error = '邮箱被禁止注册！'; break;
            case -8:  $error = '邮箱被占用！'; break;
            case -9:  $error = '手机格式不正确！'; break;
            case -10: $error = '手机被禁止注册！'; break;
            case -11: $error = '手机号被占用！'; break;
            default:  $error = '未知错误';
        }
        return $error;
    }
}